package com.yhs.YShop.redisUtil;

import java.time.Duration;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

//@Component
public class RedisUtils {

	@Autowired
	@Qualifier("stringRedisTemplate")
	private StringRedisTemplate _stringRedisTemplate;

	private static StringRedisTemplate stringRedisTemplate;
	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate _redisTemplate;

	private static RedisTemplate redisTemplate;

	@PostConstruct
	public void setRedisUtils() {
		this.stringRedisTemplate = _stringRedisTemplate;
		this.redisTemplate = _redisTemplate;
	}

	/**
	 * 设置一个值
	 * 
	 * @param key
	 * @param value
	 */
	public static void set(String key, String value) {
		stringRedisTemplate.opsForValue().set(key, value);
	}

	/**
	 * 设置一个时间的值
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 */
	public static void set(String key, String value, Duration timeout) {
		stringRedisTemplate.opsForValue().set(key, value, timeout);
	}

	/**
	 * 获取一个key的值
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		if (stringRedisTemplate.hasKey(key)) {
			return stringRedisTemplate.opsForValue().get(key);
		} else {
			return null;
		}

	}

}
